
<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属功能" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="删除人" prop="updateByName">
        <el-input
          v-model="queryParams.updateByName"
          placeholder="请输入删除人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 220px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="bathDownload"
        >批量下载</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="bathDelete"
        >彻底删除</el-button>
      </el-col>
    </el-row>
    <div class="tablebox">
      <el-table
        stripe
        :data="tableData"
        v-loading="loading"
        ref="multipleTable"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        @sort-change="tableSortChange"
        :default-sort="{prop: 'updateTime', order: 'descending'}"
      >
        <el-table-column
          type="selection"
          width="55"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="文件名称"
          align="left"
          prop="fileName"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="所属项目"
          align="left"
          prop="projectName"
        />
        <el-table-column
          label="所属业务"
          align="left"
          prop="businessName"
        />
        <el-table-column
          label="所属功能"
          prop="category"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          align="center"
          width="100"
        />
        <el-table-column
          label="删除人"
          prop="updateByName"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          align="center"
          width="100"
        />
        <el-table-column
          label="删除时间"
          prop="updateTime"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column width="150" label="操作">
          <template slot-scope="scope">
            <a @click="handlePreview(scope.row)">
              <i class="el-icon-search aicon"></i>预览
            </a>
            <a @click="downloadOne(scope.row.guid,scope.row.fileName)">
              <i class="el-icon-download aicon"></i>下载
            </a>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import aTitle from '@/components/Title';
import {
  listFileRecycle,
  delFileRecycle
} from '@/api/projectleader/fileRecycle';
import { handleBatchDownload } from '@/api/zipdownload';
import axios from 'axios'
import { getToken } from '@/utils/auth'
export default {
  name: "FileRecycle",
  components: { aTitle },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      total: 0,
      loading: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,
        category: undefined,
        updateByName: undefined,
        updateTime: undefined
      },
      tableData: [],
      multiple: true,
      multipleSelection: [],
      statusOptions: [],
      // 日期范围
      dateRange: [],
      fileNames: [],
      files: []
    }
  },
  watch: {
    multipleSelection (val) {
      if (val.length > 0) {
        this.multiple = false
      } else {
        this.multiple = true
      }
    }
  },
  created () {
    this.getList()
    //附件类别
    this.getDicts('sys_attachment_category').then(response => {
      this.statusOptions = response.data
    })
    this.queryParams.leader = this.$route.params.id;
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}项目负责人工作台/业务系统操作说明-项目负责人工作台-文档回收站.xlsx`
      )}&name=${encodeURIComponent("业务系统操作说明-项目负责人工作台-文档回收站.xlsx")}`;
    },
    getList () {
      listFileRecycle(this.addDateRange(this.queryParams, this.dateRange)).then(
        res => {
          this.total = res.total
          this.tableData = res.rows.map(item => {
            switch (item.category) {
              case '00':
                item.category = '便签';
                break
              case '01':
                item.category = '项目概况';
                break
              case '02':
                item.category = '收资管理';
                break
              case '03':
                item.category = '项目文档';
                break
              case '04':
                item.category = '资料归档';
                break
              case '05':
                item.category = '成果校审';
                break
              case '06':
                item.category = '个人/企业专家';
                break
              case '07':
                item.category = '典型案例';
                break
              case '08':
                item.category = '业务手册';
                break
              case '09':
                item.category = '任务模板';
                break
              case '10':
                item.category = '常用文件';
                break
            }
            return item
          })
          this.loading = false
        }
      )
    },
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.files = val
      this.ids = val.map(item => item.guid)
      this.fileNames = val.map(item => item.fileName)
      this.multipleSelection = val
    },
    handlePreview (row) {
      this.filePreview(row.fileName, row.storagePath)
    },
    resetQuery () {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 批量下载
    bathDownload () {
      const filesList = []
      this.files.map(item => {
        filesList.push({
          fileurl: '/api/system/download/' + item.guid,
          fileName: item.fileName
        })
      })
      handleBatchDownload(filesList)
    },
    downloadOne (id, name) {
      this.downloadBlob(id, name)
    },
    // 批量删除
    bathDelete (row) {
      const ids = row.guid || this.ids
      const fileNames = row.fileName || this.fileNames
      this.$confirm('确认要永久性地删除文件"' + fileNames + '"?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delFileRecycle(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () { })
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
}
</script>
<style lang="scss" scoped>
.el-date-editor--daterange.el-input,
.el-date-editor--daterange.el-input__inner,
.el-date-editor--timerange.el-input,
.el-date-editor--timerange.el-input__inner {
  width: 100%;
}

.tablebox {
  height: 100%;
}
/deep/.el-table .cell {
  font-family: PingFangSC-Regular;
  color: rgba(0, 0, 0, 0.85);
  letter-spacing: 0;
  // text-align: center;
  a {
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #1890ff;
    letter-spacing: 0;
    text-align: right;
    line-height: 14px;
    margin: 0 5px;
  }
}
</style>

